//
//  YXHomeViewController.swift
//  YXweibo
//
//  Created by AS on 5/10/16.
//  Copyright © 2016 AS. All rights reserved.
//

import UIKit

class YXHomeViewController: YXBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNavigationBar()
           }
    // 设置导航栏
    private func setupNavigationBar()
    {
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: #selector(YXHomeViewController.didClickFriendsearch))
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(YXHomeViewController.didClickPop))
        
       
        // 获取用户的名称
        let name = YXUserAccountViewModel.sharedInstance.userAccount?.screen_name ?? "未知名称"
        // 设置首页titleView
        let button = YXHomeTitleButton()
        button.setTitle(name, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(16)
        button.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        button.addTarget(self, action: #selector(YXHomeViewController.didClickTitlView(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        button.sizeToFit()
        navigationItem.titleView = button
    }
    
    // MARK: 按钮点击事件
    func didClickTitlView(button:UIButton){
        // 三角形旋转依靠button.selected这个属性来确定
        // 1.修改按钮的状态
        button.selected = !button.selected
        /*
         UIView动画旋转
         1.哪边近走那边
         2.2边一样就顺时针
         */
        // 2.根据状态旋转图片
        //        if button.selected {
        //            // 箭头转上去
        //            UIView.animateWithDuration(0.25, animations: { () -> Void in
        //                button.imageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001))
        //            })
        //        } else {
        //            UIView.animateWithDuration(0.25, animations: { () -> Void in
        //                // 箭头转下来, 清空transform
        //                button.imageView?.transform = CGAffineTransformIdentity
        ////                button.imageView?.transform = CGAffineTransformRotate(button.imageView!.transform, CGFloat(M_PI))
        //            })
        //        }
               UIView.animateWithDuration(0.25) { () -> Void in
            let transfrom = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
            button.imageView?.transform = transfrom
           
        }

    }

    func didClickFriendsearch() {
        YXPrint(items: "点击了左边")
    }
    
    func didClickPop() {
        YXPrint(items: "点击了右边")
    }

 
}
